package com.dingceng.demo.swipecard;

import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import com.dingceng.demo.photo.R;
import com.dingceng.demo.swipecard.bean.Data;
import com.dingceng.demo.swipecard.interfaces.OnSwipeCardChange;
import com.dingceng.demo.swipecard.view.SwipeCardView;

import java.util.ArrayList;
import java.util.List;

public class CardActivity extends AppCompatActivity {
    
    private SwipeCardView swipeCardView;
    private TextView txtTitle;
    private Button btnReset;
    
    List<Data> dataList;
    List<Data> moreDataList;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_card);
        
        swipeCardView = findViewById(R.id.swipeCardView);
        txtTitle = findViewById(R.id.txt_title);
        btnReset = findViewById(R.id.btn_reset);
        
        //准备数据
        initData();
        
        //绑定事件
        setEvents();
    }
    
    private void initData() {
        //准备数据
        dataList = new ArrayList<>();
        //俩占位
        dataList.add(new Data("", R.mipmap.img_music_default));
        dataList.add(new Data("", R.mipmap.img_music_default));
        //实际数据
        dataList.add(new Data("Android", R.mipmap.img_music_res_1));
        dataList.add(new Data("CCSoo", R.mipmap.img_music_res_2));
        dataList.add(new Data("Music", R.mipmap.img_music_res_3));
        dataList.add(new Data("Google", R.mipmap.img_music_res_4));
        dataList.add(new Data("Apple", R.mipmap.img_music_res_5));
        dataList.add(new Data("Window", R.mipmap.img_music_res_6));
        dataList.add(new Data("Plus", R.mipmap.img_music_res_7));
        dataList.add(new Data("Origin", R.mipmap.img_music_res_8));
        dataList.add(new Data("Dance", R.mipmap.img_music_res_9));
        dataList.add(new Data("Hello", R.mipmap.img_music_res_10));
        swipeCardView.setDataList(dataList);
    }
    
    private int loadMoreTimes = 1;
    
    private void setEvents() {
        //重置数据按钮
        btnReset.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                loadMoreTimes = 1;
                initData();
            }
        });
        
        swipeCardView.setOnSwipeCardChange(new OnSwipeCardChange() {
            @Override
            public void onChange(Data data) {
                txtTitle.setText(data.getTitle());
            }
            
            @Override
            public boolean onLoadMore() {
                //模拟异步加载
                
                //等待3秒后加载数据
                new Handler(Looper.getMainLooper()).postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        //准备即将加载的更多数据
                        loadMoreTimes++;
                        moreDataList = new ArrayList<>();
                        moreDataList.add(new Data("Android " + loadMoreTimes, R.mipmap.img_music_res_1));
                        moreDataList.add(new Data("CCSoo " + loadMoreTimes, R.mipmap.img_music_res_2));
                        moreDataList.add(new Data("Music " + loadMoreTimes, R.mipmap.img_music_res_3));
                        moreDataList.add(new Data("Google " + loadMoreTimes, R.mipmap.img_music_res_4));
                        moreDataList.add(new Data("Apple " + loadMoreTimes, R.mipmap.img_music_res_5));
                        moreDataList.add(new Data("Window " + loadMoreTimes, R.mipmap.img_music_res_6));
                        moreDataList.add(new Data("Plus " + loadMoreTimes, R.mipmap.img_music_res_7));
                        moreDataList.add(new Data("Origin " + loadMoreTimes, R.mipmap.img_music_res_8));
                        moreDataList.add(new Data("Dance " + loadMoreTimes, R.mipmap.img_music_res_9));
                        moreDataList.add(new Data("Hello " + loadMoreTimes, R.mipmap.img_music_res_10));
                        
                        swipeCardView.loadMoreDataList(moreDataList);
                    }
                }, 3000);
                
                //返回 true 代表还有数据，false 会触发已经没有新的歌曲提示
                return true;
            }
        });
    }
}